
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;
using Yozeev.Common;
using Abp.Authorization.Users;
using Microsoft.AspNetCore.Mvc;

namespace Yozeev.BusinessLogic
{
	/// <summary>
	/// ActivityTemp应用层服务的接口实现方法  
	///</summary>
	[AbpAuthorize]
	public class ActivityTempAppService : YozeevAppServiceBase, IActivityTempAppService
    {
        private readonly IRepository<ActivityTemp, Guid> _entityRepository;

        private readonly IActivityTempManager _entityManager;

		private readonly IQueryHelper _queryHelper;

		private readonly IRepository<Customer, Guid> _customerRepository;

		private readonly IRepository<Schedule, Guid> _scheduleRepository;

		/// <summary>
		/// 构造函数 
		///</summary>
		public ActivityTempAppService(
        IRepository<ActivityTemp, Guid> entityRepository
        ,IActivityTempManager entityManager
			, IQueryHelper queryHelper
			, IRepository<Customer, Guid> customerRepository
			, IRepository<Schedule, Guid> scheduleRepository
		)
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_queryHelper = queryHelper;
			_customerRepository = customerRepository;
			_scheduleRepository = scheduleRepository;
		}


		/// <summary>
		/// 获取ActivityTemp的分页列表信息
		///</summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<PagedResultDto<ActivityTempListDto>> GetPaged(GetActivityTempsInput input)
		{

			var expression = _queryHelper.WhereExp<ActivityTemp>(input.queryData);
			if (AbpUserBase.AdminUserName != GetCurrentUser().UserName)
				expression = _queryHelper.SetWhereExp<ActivityTemp>(expression, GetViewTypeEnum<ActivityTemp>(), await GetBranchUser());
			var query = from actity in _entityRepository.GetAll().AsNoTracking().Where(expression)
						join schedule in _scheduleRepository.GetAll() on actity.ScheduleId equals schedule.Id
						join customer in _customerRepository.GetAll() on actity.CustomerId equals customer.Id
						select new
						{
							actity,
							actity.Source,
							schedule,
							schedule.Route,
							schedule.Boat,
							actity.PayMethod,
							customer,
						};
			// TODO:根据传入的参数添加过滤条件
			var list = new List<ActivityTempListDto>();
			query.ToList().ForEach(temp => {
				var _actity = temp.actity.MapTo<ActivityTempListDto>();
				_actity.Source = temp.Source;
				_actity.Schedule = temp.schedule;
				_actity.Schedule.Route = temp.Route;
				_actity.Schedule.Boat = temp.Boat;
				_actity.PayMethod = temp.PayMethod;
				_actity.Customer = temp.customer;
				list.Add(_actity);
			});
			// TODO:根据传入的参数添加过滤条件
			var count = list.Count();
			list = list.AsQueryable().OrderBy(input.Sorting)
					.PageBy(input)
					.ToList();
			var entityList = list;

			var entityListDtos = entityList.MapTo<List<ActivityTempListDto>>();

			return new PagedResultDto<ActivityTempListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取ActivityTempListDto信息
		/// </summary>
		public async Task<ActivityTempListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<ActivityTempListDto>();
		}

		/// <summary>
		/// 获取编辑 ActivityTemp
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetActivityTempForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetActivityTempForEditOutput();
			ActivityTempEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<ActivityTempEditDto>();

				//activityTempEditDto = ObjectMapper.Map<List<activityTempEditDto>>(entity);
			}
			else
			{
				editDto = new ActivityTempEditDto();
			}

			output.ActivityTemp = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改ActivityTemp的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateActivityTempInput input)
		{

			if (input.ActivityTemp.Id.HasValue)
			{
				await Update(input.ActivityTemp);
			}
			else
			{
				await Create(input.ActivityTemp);
			}
		}


		/// <summary>
		/// 新增ActivityTemp
		/// </summary>
		protected virtual async Task<ActivityTempEditDto> Create(ActivityTempEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <ActivityTemp>(input);
            var entity=input.MapTo<ActivityTemp>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<ActivityTempEditDto>();
		}

		/// <summary>
		/// 编辑ActivityTemp
		/// </summary>
		protected virtual async Task Update(ActivityTempEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除ActivityTemp信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}


		/// <summary>
		/// 批量删除ActivityTemp的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出ActivityTemp为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


